package org.sam.swing;

import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import java.awt.Window;

import javax.swing.JButton;
import javax.swing.JPanel;

/**
 * 生成互操作对话框窗口
 * @author sam
 *
 */
public abstract class JSConfirmDialog extends JSDialog implements CompentBuilder {

	private static final long serialVersionUID = -7236196432243929113L;

	/** 
	 * {@inheritDoc}
	 */
	public JSConfirmDialog() {
		this((Frame) null, false);
	}

	/**
	 * {@inheritDoc}
	 */
	public JSConfirmDialog(Frame owner) {
		this(owner, false);
	}

	/**
	 * {@inheritDoc}
	 */
	public JSConfirmDialog(Frame owner, boolean modal) {
		this(owner, "", modal);
	}

	/**
	 * {@inheritDoc}
	 */
	public JSConfirmDialog(Frame owner, String title) {
		this(owner, title, false);
	}

	/**
	 * {@inheritDoc}
	 */
	public JSConfirmDialog(Frame owner, String title, boolean modal) {
		super(owner, title, modal);
		initCompoent();
	}

	/**
	 * {@inheritDoc}
	 */
	public JSConfirmDialog(Frame owner, String title, boolean modal, GraphicsConfiguration gc) {
		super(owner, title, modal, gc);
		initCompoent();
	}

	/**
	 * {@inheritDoc}
	 */
	public JSConfirmDialog(Dialog owner) {
		this(owner, false);
	}

	/**
	 * {@inheritDoc}
	 */
	public JSConfirmDialog(Dialog owner, boolean modal) {
		this(owner, "", modal);
	}

	/**
	 * {@inheritDoc}
	 */
	public JSConfirmDialog(Dialog owner, String title) {
		this(owner, title, false);
	}

	/**
	 * {@inheritDoc}
	 */
	public JSConfirmDialog(Dialog owner, String title, boolean modal) {
		super(owner, title, modal);
		initCompoent();
	}

	/**
	 * {@inheritDoc}
	 */
	public JSConfirmDialog(Dialog owner, String title, boolean modal, GraphicsConfiguration gc) {
		super(owner, title, modal, gc);
		initCompoent();
	}

	/**
	 * {@inheritDoc}
	 */
	public JSConfirmDialog(Window owner) {
		this(owner, Dialog.ModalityType.MODELESS);
	}

	/**
	 * {@inheritDoc}
	 */
	public JSConfirmDialog(Window owner, ModalityType modalityType) {
		this(owner, "", modalityType);
	}

	/**
	 * {@inheritDoc}
	 */
	public JSConfirmDialog(Window owner, String title) {
		this(owner, title, Dialog.ModalityType.MODELESS);
	}

	/**
	 * {@inheritDoc}
	 */
	public JSConfirmDialog(Window owner, String title, Dialog.ModalityType modalityType) {
		super(owner, title, modalityType);
		initCompoent();
	}

	/**
	 * {@inheritDoc}
	 */
	public JSConfirmDialog(Window owner, String title, Dialog.ModalityType modalityType, GraphicsConfiguration gc) {
		super(owner, title, modalityType, gc);
		initCompoent();
	}

	/**
	 * 确定操作
	 */
	public static final String OK = "ok";

	/**
	 * 取消操作
	 */
	public static final String CANCEL = "cancel";
	
	/**
	 * 执行出错
	 */
	public static final String ERROR = "error";

	/**
	 * 当前的执行动作
	 */
	private String action = CANCEL;

	/**
	 * 当前的执行动作
	 * 
	 * @return
	 */
	public String getAction() {
		return action;
	}

	/**
	 * 当前的执行动作
	 * 
	 * @param action
	 */
	public void setAction(String action) {
		this.action = action;
	}
	
	/**
	 * 初始化控件显示
	 */
	protected void initCompoent(){
		this.setLayout(new BorderLayout());
		JPanel centerPanel = this.buildCompent();
		if (centerPanel != null){
			this.add(centerPanel , BorderLayout.CENTER);
		}
		JPanel actionPanel = this.buildActionCompent();
		if (actionPanel != null){
			this.add(actionPanel , BorderLayout.SOUTH);
		}
		
		this.setSize(640, 480);
		this.setLocationRelativeTo(null);
	}

	/**
	 * {@inheritDoc}
	 * 生成中央对象
	 */
	@Override
	public abstract JPanel buildCompent();

	/**
	 * {@inheritDoc}
	 * 生成底部按钮
	 */
	@Override
	public JPanel buildActionCompent() {
		JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
		
		JButton btnOk = new JButton("确定");
		btnOk.setName("btnOk");
		btnOk.addActionListener(e -> {
			JSConfirmDialog.this.setAction(OK);
		});
		panel.add(btnOk);
		
		JButton btnCancel = new JButton("取消");
		btnCancel.setName("btnCancel");
		btnCancel.addActionListener(e -> {
			JSConfirmDialog.this.setAction(CANCEL);
			JSConfirmDialog.this.setVisible(false);
		});
		panel.add(btnCancel);
		
		return panel;
	}
}
